<template>
  <div class="forget-password">
    <NuxtLink to="/">
      <span class="icon-close iconfont close" />
    </NuxtLink>
    <div class="header flex flex-column justify-center align-center">
      <span class="logo" />
      <div class="f-16">修改密码</div>
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="phone"
        clearable
        :error="false"
        name="phone"
        label=""
        placeholder="输入手机号"
        :rules="[
          { required: true, message: '请输入手机号' },
          {
            required: true,
            validator: phoneValidator,
            message: '请输入正确的手机号',
          },
        ]"
      />
      <van-field
        v-model="sms"
        center
        :error="false"
        clearable
        name="sms"
        placeholder="请输入短信验证码"
        :rules="[{ required: true, message: '请输入短信验证码' }]"
      >
        <template #button>
          <van-button size="small" type="warning" @click="getVcode">
            {{ vcodeMsg }}
          </van-button>
        </template>
      </van-field>
      <van-field
        v-model="password"
        clearable
        :error="false"
        name="password"
        label=""
        type="password"
        placeholder="输入新的密码"
        :rules="[
          { required: true, message: '请输入密码' },
          {
            required: true,
            validator: passwordLengthValidator,
            message: '密码长度为至少为6位',
          },
        ]"
      />
      <van-field
        v-model="confirmPassword"
        clearable
        type="password"
        :error="false"
        name="confirmPassword"
        label=""
        placeholder="确认新的密码"
        :rules="[
          { required: true, message: '请输入确认密码' },
          {
            required: true,
            validator: passwordLengthValidator,
            message: '密码长度为至少为6位',
          },
          {
            required: true,
            validator: repeatPassword,
            message: '确认密码应与密码一致',
          },
        ]"
      />
      <van-button
        size="small"
        class="modify-password-btn"
        round
        block
        ative-type="submit"
      >
        确认
      </van-button>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      phone: "",
      sms: "",
      password: "",
      confirmPassword: "",
      vcodeMsg: "发送验证码",
    };
  },
  methods: {
    // 获取短信验证码
    getVcode() {
      if (this.timer) {
        return;
      }
      let count = 60;
      this.vcodeMsg = count + "s";
      this.timer = setInterval(() => {
        if (count <= 0) {
          this.vcodeMsg = "发送验证码";
          clearInterval(this.timer);
          this.timer = null;
          return;
        }
        count--;
        this.vcodeMsg = count + "s";
      }, 1000);
      this.$once("hook:beforeDestroy", function () {
        clearInterval(this.timer);
        this.timer = null;
      });
    },
    // 手机号校验
    phoneValidator(value, rule) {
      const reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      return reg_tel.test(value);
    },
    // 密码长度校验
    passwordLengthValidator(value, rule) {
      return value.length >= 6;
    },
    // 确认密码一致校验
    repeatPassword(value, rule) {
      return value === this.password;
    },
    onSubmit(values) {
      console.log("submit", values);
      this.$axios.put("/v1/user/modifyPassword", values).then((res) => {
        if (res && res.data.code == 200) {
          this.$toast.success("修改成功");
          this.$router.replace("/login/accountPasswordLogin");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.forget-password {
  padding: 10px;
  .close {
    font-size: 25px;
  }
  .header {
    margin-bottom: 50px;
    .logo {
      margin-bottom: 10px;
      width: 50px;
      height: 50px;
      background: url("@/assets/imgs/logo.jpg");
      background-size: 100% 100%;
    }
  }
  .modify-password-btn {
    font-size: 14px;
    margin-top: 20px;
    color: #f55555;
    border-color: #f55555;
    background: #fff;
  }
  .msg-login {
    font-size: 14px;
    margin-top: 20px;
    color: #f55555;
    border-color: #f55555;
    background: #fff;
  }
}
</style>